SQL All-in-One For Dummies by Taylor Allen G
Author:Taylor, Allen G.
Language: eng
Format: epub
Publisher: John Wiley & Sons, Inc.
Published: 2011-03-03T16:00:00+00:00
You can’t fetch rows from a cursor until you open the cursor. When you open a cursor, the values of variables referenced in the DECLARE CURSOR statement become fixed, as do all current datetime functions. Consider the following example of SQL statements embedded in a host language program:
EXEC SQL DECLARE CURSOR C1 FOR SELECT * FROM ORDERS
WHERE ORDERS.Customer = :NAME
AND DueDate < CURRENT_DATE ;
NAME := ‘Acme Co’; //A host language statement
EXEC SQL OPEN C1;
NAME := ‘Omega Inc.’; //Another host statement
...
EXEC SQL UPDATE ORDERS SET DueDate = CURRENT_DATE;
The OPEN statement fixes the value of all variables referenced in the DECLARE CURSOR statement and also fixes a value for all current datetime functions. Thus the second assignment to the name variable (NAME := ‘Omega Inc.’) has no effect on the rows that the cursor fetches. (That value of NAME is used the next time you open C1.) And even if the OPEN statement is executed a minute before midnight and the UPDATE statement is executed a minute after midnight, the value of CURRENT_DATE in the UPDATE statement is the value of that function at the time the OPEN statement executed. This is true even if DECLARE CURSOR doesn’t reference the date-time function.
Operating on a Single Row
Whereas the DECLARE CURSOR statement specifies the cursor’s name and scope, and the OPEN statement collects the table rows selected by the DECLARE CURSOR query expression, the FETCH statement actually retrieves the data. The cursor may point to one of the rows in the cursor’s scope, or to the location immediately before the first row in the scope, or to the location immediately after the last row in the scope, or to the empty space between two rows. You can specify where the cursor points with the orientation clause in the FETCH statement.
FETCH syntax
The syntax for the FETCH statement is
FETCH [[orientation] FROM] cursor-name
INTO target-specification [, target-specification]... ;
Seven orientation options are available:
♦ NEXT
♦ PRIOR
♦ FIRST
♦ LAST
♦ ABSOLUTE
♦ RELATIVE
♦ <simple value specification>
The default option is NEXT, which was the only orientation available in versions of SQL prior to SQL-92. It moves the cursor from wherever it is to the next row in the set specified by the query expression. If the cursor is located before the first record, it moves to the first record. If it points to record n, it moves to record n+1. If the cursor points to the last record in the set, it moves beyond that record, and notification of a no data condition is returned in the SQLSTATE system variable. (Book IV, Chapter 4 details SQLSTATE and the rest of SQL’s error-handling facilities.)
The target specifications are either host variables or parameters, respectively, depending on whether embedded SQL or module language is using the cursor. The number and types of the target specifications must match the number and types of the columns specified by the query expression in the DECLARE CURSOR statement. So in the case of embedded SQL, when you fetch a list of five values from a row of
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
NET | C & C++ Windows Programming |
SQL Server | VBA |
Visual Basic |
Deep Learning with Python by François Chollet(12520)
Hello! Python by Anthony Briggs(9867)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9757)
The Mikado Method by Ola Ellnestam Daniel Brolund(9747)
Dependency Injection in .NET by Mark Seemann(9293)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7741)
Grails in Action by Glen Smith Peter Ledbrook(7667)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7517)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6743)
Microservices with Go by Alexander Shuiskov(6510)
Practical Design Patterns for Java Developers by Miroslav Wengner(6408)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6386)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6378)
Angular Projects - Third Edition by Aristeidis Bampakos(5765)
The Art of Crafting User Stories by The Art of Crafting User Stories(5296)
NetSuite for Consultants - Second Edition by Peter Ries(5241)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5058)
Kotlin in Action by Dmitry Jemerov(5019)
